package comg.springboot.log.demo.pojo;

import com.fasterxml.jackson.annotation.JsonFormat;
import comg.springboot.log.demo.util.Utils;
import lombok.Data;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.Date;
import java.util.Map;

/**
 * @Author: jxm
 * @Description:
 * @Date: 2022/4/15 14:31
 * @Version: 1.0
 */
@Data
public class Log implements Serializable {

    private String logId;           //日志主键

    private String type;            //日志类型

    private String title;           //日志标题

    private String remoteAddr;          //请求地址

    private String requestUri;          //URI

    private String method;          //请求方式

    private String params;          //提交参数

    private String exception;           //异常

    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")

    private Date operateDate;           //开始时间

    private String timeout;         //结束时间

    private String userId;          //用户ID

    private String resultParams;    //返回参数


    /**
     * 设置请求参数
     * @param paramMap
     */
    public void setMapToParams(Map<String,String[]> paramMap){
        if(paramMap == null){
            return;
        }
        StringBuilder params = new StringBuilder();
        for(Map.Entry<String, String[]> param : ((Map<String, String[]>)paramMap).entrySet()){
            params.append(("".equals(param) ? "" : "&") + param.getKey() + "=");
            //(param.getValue() != null && param.getValue().length > 0 ? param.getValue()[0] : "");
            String paramValue = (param.getValue() != null && param.getValue().length > 0 ? param.getValue()[0] : "");
            params.append(Utils.abbr(StringUtils.endsWithIgnoreCase(param.getKey(), "password") ? "" : paramValue, 100));
        }
        this.params = params.toString();
    }

}
